package com.example.controller;

import com.example.common.Result;
import com.example.dto.GoodsPageDTO;
import com.example.entity.GoodsInfo;
import com.example.entity.Account;
import com.example.entity.TypeInfo;
import com.example.service.GoodsInfoService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping(value = "/goodsInfo")
@Api(tags = "菜品模块")
public class GoodsInfoController {
    @Resource
    private GoodsInfoService goodsInfoService;

//    @PostMapping
//    public Result<GoodsInfo> add(@RequestBody GoodsInfo goodsInfo, HttpServletRequest request) {
//        Account user = (Account) request.getSession().getAttribute("user");
//        goodsInfo.setUserId(user.getId());
//        goodsInfo.setLevel(user.getLevel());
//        goodsInfo.setCount(10000);
//        goodsInfoService.add(goodsInfo);
//        return Result.success(goodsInfo);
//    }

    /**
     * 添加菜品
     * @param goodsInfo 新菜品信息
     * @return
     */
    @ApiOperation("添加菜品")
    @PostMapping()
    public Result<GoodsInfo> add(@RequestBody GoodsInfo goodsInfo) {
        GoodsInfo add = this.goodsInfoService.add(goodsInfo);
        return Result.success(add);
    }

    /**
     * 根据主键删除菜品
     * @param id
     * @return
     */
    @ApiOperation("根据主键删除菜品")
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Long id) {
        goodsInfoService.delete(id);
        return Result.success();
    }

    /**
     * 更新菜品信息
     * @param goodsInfo
     * @return
     */
    @ApiOperation("更新菜品信息（含主键）")
    @PutMapping
    public Result update(@RequestBody GoodsInfo goodsInfo) {
        this.goodsInfoService.update(goodsInfo);
        return Result.success();
    }

    /**
     * 根据主键查询菜品信息
     * @param id
     * @return
     */
    @ApiOperation("根据主键查询菜品信息")
    @GetMapping("/{id}")
    public Result<GoodsInfo> detail(@PathVariable Long id) {
        GoodsInfo goodsInfo = goodsInfoService.findById(id);
        return Result.success(goodsInfo);
    }

    /**
     * 查询所有菜品信息
     * @return
     */
    @ApiOperation("查询所有菜品信息")
    @GetMapping
    public Result<List<GoodsInfo>> all() {
        return Result.success(goodsInfoService.findAll());
    }

//    @GetMapping("/page/{name}")
//    public Result<PageInfo<GoodsInfo>> page(@RequestParam(defaultValue = "1") Integer pageNum,
//                                            @RequestParam(defaultValue = "10") Integer pageSize,
//                                            @PathVariable String name,
//                                            HttpServletRequest request) {
//        return Result.success(goodsInfoService.findPage(pageNum, pageSize, name, request));
//    }

    /**
     * PageHelper获取菜品种类（分页）
     * @param goodsPageDTO
     * @return
     */
    @ApiOperation("PageHelper获取菜品列表（分页）")
    @PostMapping("/page")
    public Result<PageInfo<GoodsInfo>> page(@RequestBody GoodsPageDTO goodsPageDTO) {
        PageInfo<GoodsInfo> byPage = this.goodsInfoService.getByPage(goodsPageDTO);
        return Result.success(byPage);
    }

    /**
     * 根据菜品种类查询菜品
     * @param typeId
     * @return
     */
    @ApiOperation("根据菜品种类查询菜品")
    @GetMapping("/findByType/{typeId}")
    public Result<List<GoodsInfo>> findByType(@PathVariable Integer typeId) {
        return Result.success(goodsInfoService.findByType(typeId));
    }

//    @GetMapping("/recommend")
//    public Result<PageInfo<GoodsInfo>> recommendGoods(@RequestParam(defaultValue = "1") Integer pageNum,
//                                                      @RequestParam(defaultValue = "10") Integer pageSize) {
//        return Result.success(goodsInfoService.findRecommendGoods(pageNum, pageSize));
//    }
//
//    @GetMapping("/sales")
//    public Result<PageInfo<GoodsInfo>> sales(@RequestParam(defaultValue = "1") Integer pageNum,
//                                             @RequestParam(defaultValue = "10") Integer pageSize) {
//        return Result.success(goodsInfoService.findHotSalesGoods(pageNum, pageSize));
//    }

//    /**
//     * 查询用户买到过的所有菜品
//     * @return
//     */
//    @GetMapping("/comment/{userId}/{level}")
//    public Result<List<GoodsInfo>> orderGoods(@PathVariable("userId") Long userId,
//                                              @PathVariable("level") Integer level) {
//        return Result.success(goodsInfoService.getOrderGoods(userId, level));
//    }

//    @GetMapping("/searchGoods")
//    public Result<List<GoodsInfo>> searchGoods(@RequestParam String text) {
//        return Result.success(goodsInfoService.searchGoods(text));
//    }
}
